<?php
defined('IN_LM') or die('Error -1 SQL'); //防止非法调用


class Database {
    private $pdo;

    public function __construct($host, $dbname, $username, $password) {
        try {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo '连接失败: ' . $e->getMessage();
        }
    }

    public function read_data_on_tab($tabname, $where=array(), $order=array(), $len="") 
    {
        $sql = "SELECT * FROM $tabname";
        if (!empty($where)) {

            $a=count($where);
            $keys=array_keys($where);
            $execut=array();
            for($b=0;$b<$a;$b++)
            {
                if($b!=$a-1)
                {
                    $w.=$keys[$b]."=:".$keys[$b]." AND ";
                }else
                {
                    $w.=$keys[$b]."=:".$keys[$b]." ";
                }

                $execut[':'.$keys[$b]]=$where[$keys[$b]];
            }

            $sql .= " WHERE ".$w;
        }
        if(!empty($order))
        {
            $a=count($order);
            $keys=array_keys($order);
            for($b=0;$b<$a;$b++)
            {
                if($b!=$a-1)
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]].",";
                }else
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]]." ";
                }

            }
            $sql .=" order by ".$o;                
        }

        if($len!="")
        {
            $sql.=" limit ".$len;
        }


        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($execut);
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo '查询失败: ' . $e->getMessage();
            return null;
        }

    }

    public function addend_data_on_tab($tabname,$arr)
    {

        $a=count($arr);
        $keys=array_keys($arr);
        $execut=array();
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $column.=$keys[$b].",";
                $value.=':'.$keys[$b].",";
            }else
            {
                $column.=$keys[$b]." ";
                $value.=':'.$keys[$b]." ";
            }
            $execut[':'.$keys[$b]]=$arr[$keys[$b]];
        }

        $sql="INSERT INTO ".$tabname."(".$column.") VALUES (".$value.")";

        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($execut);
            $last_id = $this->pdo->lastInsertId();
            return $last_id;
        } catch (PDOException $e) {
            echo '插入失败: ' . $e->getMessage();
            return false;
        }


    }

    public function updata_on_tab($tabname,$where,$arr)
    {
        $execut=array();
        $a=count($arr);
        $keys=array_keys($arr);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $column.=$keys[$b]."=".':'.$keys[$b].",";
            }else
            {
                $column.=$keys[$b]."=".':'.$keys[$b]." ";
            }

            $execut[':'.$keys[$b]]=$arr[$keys[$b]];

        }
        $a=count($where);
        $keys=array_keys($where);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $w.=$keys[$b]."=".':where_'.$keys[$b]." AND ";
            }else
            {
                $w.=$keys[$b]."=".':where_'.$keys[$b]." ";
            }

            $execut[':where_'.$keys[$b]]=$where[$keys[$b]];

        }
        $sql="UPDATE ".$tabname." set ".$column." WHERE ".$w;
        
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($execut);
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo '修改失败: ' . $e->getMessage();
            return false;
        }
    }

    function deldata_on_tab($tabname,$where)
    {
        $execut=array();
        $a=count($where);
        $keys=array_keys($where);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $w.=$keys[$b]."=".':'.$keys[$b]." AND ";
            }else
            {
                $w.=$keys[$b]."=".':'.$keys[$b]." ";
            }

            $execut[':'.$keys[$b]]=$where[$keys[$b]];

        }
        $sql="DELETE FROM ".$tabname." WHERE ".$w;
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute($execut);
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo '删除失败: ' . $e->getMessage();
            return false;
        }

    }

    public function closedb()
    {
        $this->pdo = null;  // 关闭数据库连接
    }

    public function __destruct() {
        $this->closedb();
    }
}




class sqldb
{
    public $DB;
    public $sqlhead;
    function conetdb($username,$password,$dbnanme,$localhost="127.0.0.1",$port="3306")
    {
        $this->DB=new mysqli($localhost,$username,$password,$dbnanme,$port)or die("Erro from DB 0");
        $this->DB->set_charset('utf8');
    }
    function closedb()
    {
        $this->DB->close();
    }
    function count_on_tab($tabname)
    {
        $tabname=$this->sqlhead.$tabname;
        
        $sql="SELECT * FROM ".$tabname;
        $result =$this->DB->query($sql);
        $total_rows = mysqli_num_rows($result);
        //echo $sql;
        return $total_rows;

    }

    //无条件获取数据表所有数据，只注重排序和数量
    function read_alldata_on_tab($tabname,$order='',$len=0)
    {
        $tabname=$this->sqlhead.$tabname;
        //echo $tabname;
        $od='';
        if($order!='')
        {
            $a=count($order);
            $keys=array_keys($order);
            for($b=0;$b<$a;$b++)
            {
                if($b!=$a-1)
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]].",";
                }else
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]]." ";
                }

            }
            $od=" order by ".$o;
            //echo $od.'<br>';

        }
        
        $sql="SELECT *FROM ".$tabname.$od;

        
        //echo $sql;
        if($len!=0)
        {
            $sql=$sql." limit ".$len;
        }
        //echo $sql;
        $result=$this->DB->query($sql);
        if ($result->num_rows > 0)
        {
            $c=0;
            while($row = $result->fetch_assoc())
            {
                $d[$c]=$row;
                $c++;

            }
            return $d;

        }
    }

    function read_data_on_tab($tabname,$where='',$order='',$len=0)
    {
        $tabname=$this->sqlhead.$tabname;
        //echo $tabname;
        $od='';
        if($order!='')
        {
            $a=count($order);
            $keys=array_keys($order);
            for($b=0;$b<$a;$b++)
            {
                if($b!=$a-1)
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]].",";
                }else
                {
                    $o.=$keys[$b]." ".$order[$keys[$b]]." ";
                }

            }
            $od=" order by ".$o;
            //echo $od.'<br>';

        }
        if($where=='')
        {
            $sql="SELECT *FROM ".$tabname.$od;

        }else
        {
            $a=count($where);
            $keys=array_keys($where);
            for($b=0;$b<$a;$b++)
            {
                if($b!=$a-1)
                {
                    $w.=$keys[$b]."='".$where[$keys[$b]]."' AND ";
                }else
                {
                    $w.=$keys[$b]."='".$where[$keys[$b]]."' ";
                }

            }
            $sql="SELECT *FROM ".$tabname." WHERE ".$w.$od;
        }
        //echo $sql;
        if($len!=0)
        {
            $sql=$sql." limit ".$len;
        }
        //echo $sql;
        $result=$this->DB->query($sql);
        if ($result->num_rows > 0)
        {
            $c=0;
            while($row = $result->fetch_assoc())
            {
                $d[$c]=$row;
                $c++;

            }
            return $d;

        }
    }

    function addend_data_on_tab($tabname,$arr)
    {
        $tabname=$this->sqlhead.$tabname;
        $a=count($arr);
        $keys=array_keys($arr);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $column.=$keys[$b].",";
            }else
            {
                $column.=$keys[$b]." ";
            }

        }
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $value.="'".$arr[$keys[$b]]."',";
            }else
            {
                $value.="'".$arr[$keys[$b]]."' ";
            }

        }

        $sql="INSERT INTO ".$tabname."(".$column.") VALUES (".$value.")";
        //echo $sql;
        $this->DB->query($sql);
        return $this->DB->insert_id;//返回新添加条目的id
    }

    function updata_on_tab($tabname,$where,$arr)
    {
        $tabname=$this->sqlhead.$tabname;
        $a=count($arr);
        $keys=array_keys($arr);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $column.=$keys[$b]."="."'".$arr[$keys[$b]]."',";
            }else
            {
                $column.=$keys[$b]."="."'".$arr[$keys[$b]]."' ";
            }

        }
        $a=count($where);
        $keys=array_keys($where);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $w.=$keys[$b]."="."'".$where[$keys[$b]]."' AND ";
            }else
            {
                $w.=$keys[$b]."="."'".$where[$keys[$b]]."' ";
            }

        }
        $sql="UPDATE ".$tabname." set ".$column." WHERE ".$w;
        //echo $sql;
        return $result=$this->DB->query($sql);
    }
    function deldata_on_tab($tabname,$where)
    {
        $tabname=$this->sqlhead.$tabname;
        $a=count($where);
        $keys=array_keys($where);
        for($b=0;$b<$a;$b++)
        {
            if($b!=$a-1)
            {
                $w.=$keys[$b]."="."'".$where[$keys[$b]]."' AND ";
            }else
            {
                $w.=$keys[$b]."="."'".$where[$keys[$b]]."' ";
            }

        }
        $sql="DELETE FROM ".$tabname." WHERE ".$w;
        return $result=$this->DB->query($sql);
    }
}

?>